# -*- coding:utf-8 -*- #

## 日志调试模块.
# @param looger_path 完整的路径+日志名.
# @var logger_dict 字典.
# @return logger_dict[logger_path]对象.
# @see http://blog.csdn.net/fxjtoday/article/details/6307285.

import logging
import os

logger_dict = {}
def getlogger(logger_path):
    print 'something is error, lookup the logger %s' % logger_path
    
    #得到存放log的目录
    logger_path_dir = os.path.dirname(logger_path)
    #如果log目录不存在就新建
    if not os.path.isdir(logger_path_dir):
        os.makedirs(logger_path_dir)
        
    if logger_path not in logger_dict:
        # 生成一个日志对象，里面的参数时日志的名字,打印在日志中开头的字符串
        logger = logging.getLogger()
        
        # 生成一个handler,这里使用写文件FileHandler,默认当前路径(需要文件名)
        handle = logging.FileHandler(logger_path)
        
        # 定义日志输出信息的顺序,结构和内容
        # %(asctime)s 错误执行时间
        # %(module)s 使用模块名
        # %(thread)s 线程ID号
        # %(threadName)s 线程名字
        # %(lineno)s log级别(DEBUG,INFO,WARING,ERROR,CRITICAL)
        # %(levelname)s 写入日志的级别名字
        # %(message)s 错误信息
        handle.setFormatter(
                logging.Formatter(("%(asctime)s moduel:%(module)s"
                " thread:%(thread)s"
                " threadName:%(threadName)s"
                " lineno:%(lineno)s %(levelname)s:%(message)s\n"
                )))
        
        #将处理器加到日志对象上
        logger.addHandler(handle)
        
        logger_dict[logger_path] = logger
    return logger_dict[logger_path]
